//
//  XHRightViewController.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-5-23.
//  Copyright (c) 2014年 broadwave.com. All rights reserved.
//
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#import "XHRightViewController.h"
#import "AFNetworking.h"

#import "UIImageView+AFNetworking.h"
#import "AFHTTPRequestOperation.h"
#import "GDataXMLNode.h"

#define FF 65
@interface XHRightViewController ()
@property (nonatomic, strong) UILabel* tianqi;
@property (nonatomic, strong) UILabel* wendu;
@property (nonatomic, strong) UILabel* fengli;
@property (nonatomic, strong) NSMutableArray* labelArray;
@property (nonatomic, strong) UILabel* l1;
@property (nonatomic, strong) UILabel* l2;
@property (nonatomic, strong) UILabel* l3;
@property (nonatomic, strong) UILabel* l4;
@property (nonatomic, strong) UIImageView* v;
@property (nonatomic, strong) UILabel* lsf;
@end

@implementation XHRightViewController
@synthesize tianqi = _tianqi;
@synthesize wendu = _wendu;
@synthesize fengli = _fengli;
@synthesize l4= _l4;
@synthesize l3 = _l3;
@synthesize l2= _l2;
@synthesize l1= _l1;
@synthesize v = _v;
@synthesize lsf = _lsf;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
    //{
     //   self.edgesForExtendedLayout = UIRectEdgeNone;
    //}
    UIScrollView* scrol  = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height-80)];
    if (iPhone5)
    {
        scrol.frame = self.view.frame;
    }
    else
    {
        scrol.contentSize= CGSizeMake(320, 560);
    }
    scrol.bounces = NO;
    
    UIImageView* imag = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
    imag.image = [UIImage imageNamed:@"left_right_menu_bg.png"];
    imag.contentMode = UIViewContentModeScaleToFill;
    [scrol addSubview:imag];
    UILabel* lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    lable.font = [UIFont systemFontOfSize:17];
    lable.backgroundColor = [UIColor clearColor];
    lable.textColor = [UIColor whiteColor];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.center = CGPointMake(120, 45);
    //[scrol addSubview:lable];
    lable.text = @"太原";
    

    UIImageView* v = [[UIImageView alloc] initWithFrame:CGRectMake(70, 36, 110, 110)];
    v.contentMode = UIViewContentModeScaleToFill;
    v.layer.masksToBounds = YES;
    v.layer.cornerRadius = 2;
    self.v = v;
    [scrol addSubview:v];
    v.image = [UIImage imageNamed:@"base_detail_default_pic.png"];
    UILabel* fengli = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 25)];
    fengli.backgroundColor = [UIColor clearColor];
    fengli.textColor = [UIColor whiteColor];
    fengli.textAlignment = NSTextAlignmentCenter;
    fengli.font = [UIFont systemFontOfSize:13];
    fengli.center = CGPointMake(120+0,165);
    [scrol addSubview:fengli];
    fengli.text = @"我的二维码名片";
    self.fengli = fengli;
    UIView* fef = [[UIView alloc] initWithFrame:CGRectMake(30+0, 195, 200, 60)];
    fef.backgroundColor = [UIColor whiteColor];
    fef.alpha = 0.2;
   
    
    UILabel* tianqi = [[UILabel alloc] initWithFrame:CGRectMake(30, 195, 80, 60)];
    tianqi.backgroundColor = [UIColor clearColor];
    tianqi.textColor = [UIColor whiteColor];
    tianqi.textAlignment = NSTextAlignmentCenter;
    tianqi.font = [UIFont systemFontOfSize:18];
    //tianqi.center = CGPointMake(120+0,95);
    [scrol addSubview:tianqi];
    tianqi.text = @"多云";
    self.tianqi = tianqi;
    
    self.labelArray = [[NSMutableArray alloc] init];
    UILabel* wendu = [[UILabel alloc] initWithFrame:CGRectMake(110, 195, 120, 30)];
    wendu.backgroundColor = [UIColor clearColor];
    wendu.textColor = [UIColor whiteColor];
    wendu.textAlignment = NSTextAlignmentCenter;
    wendu.font = [UIFont systemFontOfSize:15];
    
    [scrol addSubview:wendu];
    //wendu.text = @"25℃-23℃";
    self.wendu = wendu;
    UILabel* lsf = [[UILabel alloc] initWithFrame:CGRectMake(110, 195+30, 120, 30)];
    lsf.backgroundColor = [UIColor clearColor];
    lsf.textColor = [UIColor whiteColor];
    lsf.textAlignment = NSTextAlignmentCenter;
    lsf.font = [UIFont systemFontOfSize:12];
    lsf.numberOfLines = 2;
    
    [scrol addSubview:lsf];
    //wendu.text = @"25℃-23℃";
    self.lsf = lsf;
    
    UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 220, 1)];
    l1.backgroundColor = [UIColor whiteColor];
    l1.center = CGPointMake(130+0, 185);
    [scrol addSubview:l1];
    UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(30+0, 195+FF, 200, 60)];
    v1.backgroundColor = [UIColor whiteColor];
    v1.alpha = 0.2;
    [scrol addSubview:v1];
     [scrol addSubview:fef];
    UIView* v2 = [[UIView alloc] initWithFrame:CGRectMake(30+0, 195+65+FF, 200, 60)];
    v2.backgroundColor = [UIColor whiteColor];
    v2.alpha = 0.2;
    [scrol addSubview:v2];
    UIView* v3 = [[UIView alloc] initWithFrame:CGRectMake(30+0, 195+125+5+FF, 200, 60)];
    v3.backgroundColor = [UIColor whiteColor];
    v3.alpha = 0.2;
    [scrol addSubview:v3];
    UIView* v4 = [[UIView alloc] initWithFrame:CGRectMake(30+0, 195+185+10+FF, 200, 60)];
    v4.backgroundColor = [UIColor whiteColor];
    v4.alpha = 0.2;
    [scrol addSubview:v4];
    NSArray* array = @[@"穿衣指数",@"感冒指数",@"运动指数",@"紫外线指数"];
    for (int i = 0; i <4 ; i++)
    {
        UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(45+0, 205+65*i+FF, 41, 41)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"weather_life_%d.png",i]];
        [scrol addSubview:imageView];
        UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(120+0, 205+65*i+FF, 100, 20)];
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentRight;
        label.font = [UIFont systemFontOfSize:16];
        label.text = array[i];
        label.textColor = [UIColor whiteColor];
        [scrol addSubview:label];
        UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(120+0, 228+65*i+FF, 100, 20)];
        l1.backgroundColor = [UIColor clearColor];
        l1.font = [UIFont systemFontOfSize:13];
        l1.textColor = [UIColor whiteColor];
        l1.textAlignment = NSTextAlignmentRight;
        // l1.text = @"良好";
        if (i ==0 )
        {
            self.l1 = l1;
        }
        else if (i == 1)
        {
            _l2 = l1;
        }
        else if (i ==2)
        {
            _l3 = l1;
        }
        else
            _l4 = l1;
        [scrol addSubview:l1];
        
    }
    [self.view addSubview:scrol];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginNotification) name:@"Login" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logoutNotification) name:@"Logout" object:nil];
    [self loadWeather];
    [self loaderweima];
    // Do any additional setup after loading the view from its nib.
}
-(void)logoutNotification
{
    self.v.image =  [UIImage imageNamed:@"base_detail_default_pic.png"];
}
-(void)loginNotification
{
    [self loaderweima];
}
-(void)loaderweima
{
    NSString* str = [NSString stringWithFormat:@"http://183.203.18.45:7001/xinhua/m/qRcideimageSave.html"];
   
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    NSString* uisd = [defaults objectForKey:@"UID"];
    if (uisd == nil)
    {
        return;
    }
    NSDictionary *params = @{@"frontUserId": uisd};
    [manager POST:str parameters:params success:^(AFHTTPRequestOperation*operation, id responseObject) {
        NSLog(@"STRING = %@",operation.responseString);
        //UIImage* iamge = [UIImage imageWithData:operation.responseData];
        NSString* imageString1 = [(NSDictionary*)operation.responseObject objectForKey:@"message"];
        NSString* imageString = [NSString stringWithFormat:@"http://183.203.18.45:7001%@",imageString1];
         [self.v setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"base_detail_default_pic"]];

        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        NSLog(@"%@",error);
        
        
    }];
    butoncl* al = [[butoncl alloc] init];
    [al postString:@"999"fff:@""];

}
-(void)loadWeather
{
    NSString* city = CITYNAME;//复制时替换 地市
    
    
    NSStringEncoding chineseEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    
    
    city = [city stringByAddingPercentEscapesUsingEncoding:chineseEncoding];
    
    NSString* str = [NSString stringWithFormat:@"http://php.weather.sina.com.cn/xml.php?city=%@&password=DJOYnieT8234jlsK&day=0",city];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        // NSLog(@"Success: %@", operation.responseString);
        
        NSString *requestTmp = [NSString stringWithString:operation.responseString];
        NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
        GDataXMLDocument *xml = [[GDataXMLDocument alloc] initWithData:resData options:0 error:nil] ;
        GDataXMLElement *rootElement = [xml rootElement];
        NSArray *weatherElements = [rootElement elementsForName:@"Weather"];
        for (GDataXMLElement* title in weatherElements)
        {
            GDataXMLElement *tianqiElement = [[title elementsForName:@"status1"] objectAtIndex:0];
            _tianqi.text  = [tianqiElement stringValue];
            GDataXMLElement* wenduElement = [[title elementsForName:@"temperature1"] objectAtIndex:0];
            GDataXMLElement* wenduElement1 = [[title elementsForName:@"temperature2"] objectAtIndex:0];
            _wendu.text = [NSString stringWithFormat:@"%@℃~%@℃",[wenduElement stringValue],[wenduElement1 stringValue]];
            GDataXMLElement* fengxiang = [[title elementsForName:@"direction1"] objectAtIndex:0];
            GDataXMLElement* fengli = [[title elementsForName:@"power1"] objectAtIndex:0];
                        GDataXMLElement* fengli1 = [[title elementsForName:@"power2"] objectAtIndex:0];
            _lsf.text = [NSString stringWithFormat:@"%@ %@ 级",[fengxiang stringValue],[fengli stringValue]];
            GDataXMLElement* chuanyi = [[title elementsForName:@"chy_l"] objectAtIndex:0];
            _l1.text = [chuanyi stringValue];
            // GDataXMLElement*
            GDataXMLElement* ganmao = [[title elementsForName:@"gm_l"] objectAtIndex:0];
            _l2.text = [ganmao stringValue];
            GDataXMLElement* ziwaixian = [[title elementsForName:@"zwx_l"] objectAtIndex:0];
            _l4.text = [ziwaixian stringValue];
            GDataXMLElement* yundong = [[title elementsForName:@"yd_l"] objectAtIndex:0];
            _l3.text = [yundong stringValue];
            
            
        }
        
        //NSLog(@"weat =%@",iconString);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure: %@ ℃", error);
    }];
    [operation start];
    
}


@end
